/*****************************************************************************
 * Copyright (c) 2019, Nations Technologies Inc.
 *
 * All rights reserved.
 * ****************************************************************************
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Nations' name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ****************************************************************************/

/**
 * @file n32g031_it.c
 * @author Nations
 * @version v1.0.0
 *
 * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
 */
#include "n32g031.h"
#include "stdio.h"
#include "stdint.h"
#include "main.h"
#include "DevicesQueue.h"
#include "DevicesUart.h"
#include "DevicesSoftTimer.h"
#include "DevicesInfrared.h"
#include "DevicesBlower.h"
#include "DevicesDelay.h"
#include "n32g031_it.h"

/** @addtogroup N32G031_StdPeriph_Template
 * @{
 */

/******************************************************************************/
/*            Cortex-M0 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
 * @brief  This function handles NMI exception.
 */
void NMI_Handler(void)
{
}

/**
 * @brief  This function handles Hard Fault exception.
 */
void HardFault_Handler(void)
{
    /* Go to infinite loop when Hard Fault exception occurs */
    while (1)
    {
    }
}

// /**
//  * @brief  This function handles SVCall exception.
//  */
// void SVC_Handler(void)
// {
// }

// /**
//  * @brief  This function handles PendSV_Handler exception.
//  */
// void PendSV_Handler(void)
// {
// }

// /**
//  * @brief  This function handles SysTick Handler.
//  */
// void SysTick_Handler(void)
// {
// }

/**
 * @brief  This function handles TIM3 global interrupt request.
 */
void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
{
    if (TIM_GetIntStatus(TIM1, TIM_INT_UPDATE) != RESET)
    {
        TIM_ClrIntPendingBit(TIM1, TIM_INT_UPDATE);
    }
}

/**
 * @brief  This function handles TIM3 global interrupt request.
 */
void TIM3_IRQHandler(void)
{
    uint8_t ucByte = 0;

    if (TIM_GetIntStatus(TIM3, TIM_INT_UPDATE) != RESET)
    {
        TIM_ClrIntPendingBit(TIM3, TIM_INT_UPDATE);

        /* 包络信号 */
        if(enumQueuePopByte(&g_TypeQueueInfraredOutput, &ucByte) == queueNormal)
        {
            switch(ucByte & 0x7F)
            {
                case INFRARED_CHANNEL_LEFT:
                    if(ucByte & 0x80)
                        INFRARED_LEFT_SET_1();
                    else
                        INFRARED_LEFT_SET_0();
                    break;

                case INFRARED_CHANNEL_RIGHT:
                    if(ucByte & 0x80)
                        INFRARED_RIGHT_SET_1();
                    else
                        INFRARED_RIGHT_SET_0();
                    break;

                case INFRARED_CHANNEL_LEFT_FRONT:
                    if(ucByte & 0x80)
                        INFRARED_LEFT_FRONT_SET_1();
                    else
                        INFRARED_LEFT_FRONT_SET_0();
                    break;

                case INFRARED_CHANNEL_RIGHT_FRONT:
                    if(ucByte & 0x80)
                        INFRARED_RIGHT_FRONT_SET_1();
                    else
                        INFRARED_RIGHT_FRONT_SET_0();
                    break;

                default : break;
            }
        }
        else
        {
            /* 关闭编码输出 */
            TIM_Enable(TIM3, DISABLE);
        }
    }
}

/**
 * @brief  This function handles TIM6 global interrupt request.
 */
void LPTIM_TIM6_IRQHandler(void)
{
    if (TIM_GetIntStatus(TIM6, TIM_INT_UPDATE) != RESET)
    {
        TIM_ClrIntPendingBit(TIM6, TIM_INT_UPDATE);

        extern volatile int64_t g_lTimerHigh;
        g_lTimerHigh += 10000;

        /* 软件定时器状态机 10ms 周期执行 */
        cSoftTimerMachine();
    }
}

/**
 * @brief  This function handles TIM3 global interrupt request.
 */
void TIM8_BRK_UP_TRG_COM_IRQHandler(void)
{
    if (TIM_GetIntStatus(TIM8, TIM_INT_UPDATE) != RESET)
    {
        /* 失能可控硅控制 */
        BLOWER_DISABLE();

        TIM_ClrIntPendingBit(TIM8, TIM_INT_UPDATE);
    }
}

void TIM8_CC_IRQHandler(void)
{
    static BlowerTypeDef *st_ptypeBlowerInfo = ptypeBlowerInfoGet();

    if (TIM_GetIntStatus(TIM8, TIM_INT_CC3) != RESET)
    {
        if(st_ptypeBlowerInfo->speedAC >= 0.1f)
        {
            /* 使能可控硅控制 */
            BLOWER_ENABLE();
        }

        /* Clear the IT pending Bit */
        TIM8->STS = (uint32_t)~TIM_INT_CC3;
    }

    if (TIM_GetIntStatus(TIM8, TIM_INT_CC4) != RESET)
    {
        /* 失能可控硅控制 */
        BLOWER_DISABLE();

        /* Clear the IT pending Bit */
        TIM8->STS = (uint32_t)~TIM_INT_CC4;
    }
}

/**
 * @brief  External lines 2 to 3 interrupt.
 */
void EXTI2_3_IRQHandler(void)
{
    static BlowerTypeDef *st_ptypeBlowerInfo = ptypeBlowerInfoGet();

    if (RESET != EXTI_GetITStatus(EXTI_LINE2))
    {
        EXTI_ClrITPendBit(EXTI_LINE2);

        /* x us后 使能可控硅控制 */
        TIM8->CCDAT3 = st_ptypeBlowerInfo->time;
        /* x + 30 us后再 失能可控硅控制 */
        TIM8->CCDAT4 = st_ptypeBlowerInfo->time + 80;
        /* Clear the Counter Register value */
        TIM8->CNT = 0;
    }
}

/**
 * @}
 */
